{
  "nbformat": 4,
  "nbformat_minor": 0,
  "metadata": {
    "colab": {
      "name": "Piano transcription inference",
      "provenance": []
    },
    "kernelspec": {
      "name": "python3",
      "display_name": "Python 3"
    },
    "accelerator": "GPU"
  },
  "cells": [
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "k43ASej1dc5N"
      },
      "source": [
        "# Install piano_transcription_inference"
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "wxRVwNOldKpc"
      },
      "source": [
        "!apt install ffmpeg wget"
      ],
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "mXoOV0fOdOFa"
      },
      "source": [
        "pip install piano_transcription_inference"
      ],
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "5kw5naQPleBr"
      },
      "source": [
        "from IPython.display import Audio"
      ],
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "GgByRF3ylK_S"
      },
      "source": [
        "# Transcribe using code"
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "_eVSRSRVdWtj"
      },
      "source": [
        "from piano_transcription_inference import PianoTranscription, sample_rate, load_audio\n",
        "\n",
        "def transcribe(audio_path, output_midi_path):\n",
        "    # Load audio\n",
        "    audio, _ = load_audio(audio_path, sr=sample_rate, mono=True)\n",
        "\n",
        "    # Transcriptor\n",
        "    transcriptor = PianoTranscription(device='cuda', checkpoint_path=None)\n",
        "\n",
        "    # Transcribe and write out to MIDI file\n",
        "    transcriptor.transcribe(audio, output_midi_path)"
      ],
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "aWNnttDblUGE"
      },
      "source": [
        "!wget https://github.com/qiuqiangkong/piano_transcription_inference/raw/master/resources/cut_liszt.mp3"
      ],
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "0h6pqv2Ulcc_"
      },
      "source": [
        "Audio('cut_liszt.mp3')"
      ],
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "fde2pTIAlkE0"
      },
      "source": [
        "transcribe('cut_liszt.mp3', 'cut_liszt.mid')"
      ],
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "JZ33qrGZuCVS"
      },
      "source": [
        "# Render midi plot"
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "RIpZKNY3uEth"
      },
      "source": [
        "!apt install git"
      ],
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "LUtmwKrWsVoA"
      },
      "source": [
        "!git clone https://github.com/bytedance/piano_transcription.git"
      ],
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "s3GHmJ5ZsWoR"
      },
      "source": [
        "pip install -r piano_transcription/requirements.txt"
      ],
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "fsgEKGE_vKgn"
      },
      "source": [
        "!mkdir results"
      ],
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "dQHtquD3rLEI"
      },
      "source": [
        "import sys\n",
        "sys.path.append('piano_transcription/utils')\n",
        "sys.path.append('piano_transcription/pytorch')\n",
        "from collections import namedtuple\n",
        "\n",
        "plot_args = namedtuple('PlotArgs', ['audio_path', 'midi_path'])\n",
        "\n",
        "from plot_for_paper import plot_midi\n",
        "plot_midi(plot_args('cut_liszt.mp3', 'cut_liszt.mid'))"
      ],
      "execution_count": null,
      "outputs": []
    }
  ]
}